﻿#include "stdafx.h"
#ifdef LK_GAME_DIRECTIONCONTROL_
namespace LK
{
    namespace Game
    {
		DirectionControl::DirectionControl()
			:pressedKeysCount(0), lastd ( -1)
        {
            KeysList[0] = KeysList[1] = KeysList[2] = KeysList[3] = 0;
			KeysValue[0] = 0x26;//VK_UP;
			KeysValue[1] = 0x28;//VK_DOWN;
			KeysValue[2] = 0x25;// VK_LEFT;
			KeysValue[3] = 0x27;// VK_RIGHT;
        }//构造函数
        DirectionControl::DirectionControl(char key_up,char key_down,char key_left,char key_right)//构造函数
			:pressedKeysCount(0), lastd(-1)
		{
            KeysList[0] = KeysList[1] = KeysList[2] = KeysList[3] = 0;
			KeysValue[0] = 0x26;
			KeysValue[1] = 0x28;
			KeysValue[2] = 0x25;
			KeysValue[3] = 0x27;
			setkey_up(key_up); setkey_down(key_down); setkey_left(key_left); setkey_right(key_right);
        }
        bool DirectionControl::KeyDown(char keyValue)//发生key按下事件时请调用此方法
        {
            tc.Lock();//临界区
            KeyUp(keyValue);
            short i=0;
			while (i<4){ if (keyValue == KeysValue[i]){ lastd = (char)i; break; }++i; }//寻找按键方向
            if(i==4)return tc.UnLock(),false;
            if(++pressedKeysCount>4)pressedKeysCount=4;
            for(i=0;i<4;++i)if(keyValue==KeysList[i])break;//寻找优先级
            if(i==4)i=3;
            while(i>=1){KeysList[i]=KeysList[i-1];--i;}//移动优先级
            KeysList[0] = keyValue;//到最上端
            tc.UnLock();
            return true;
        }
        bool DirectionControl::KeyUp(char KeyValue)//发生key弹起事件时请调用此方法
        {
            tc.Lock();//临界区
            int i = 0;
            int kc = pressedKeysCount;
            while(i<kc)
            {
                if(KeysList[i]==KeyValue)
                {
                    if(--pressedKeysCount<0)pressedKeysCount=0;
                    break;
                }
                ++i;
            }//寻找按键，找到则按下键记录减1
            if(i<kc)//找到按键
            {
                while(i<3){KeysList[i]=KeysList[i+1];++i;}//优先级移动
                KeysList[3]=KeyValue;//到底层
                tc.UnLock();
                return true;
            }
            tc.UnLock();
            return false;
        }
        char DirectionControl::CurrentDirection()//当前方向
        {
            char Value=0;
            tc.Lock();
            if(pressedKeysCount)Value = KeysList[0];//第一优先级的按键值
            tc.UnLock();
            for(int i=0;i<4;++i)if(Value==KeysValue[i])return i;
            return -1;
        }
        void DirectionControl::setkey_up(char keyValue)
        {
            tc.Lock();
            KeyUp(keyValue);
            for(int i=0;i<4;++i)
                if(KeysValue[i]==keyValue)
                    KeysValue[i]=0;
            KeysValue[0] = keyValue;
            tc.UnLock();
        }
        void DirectionControl::setkey_down(char keyValue)
        {
            tc.Lock();
            KeyUp(keyValue);
            for(int i=0;i<4;++i)
                if(KeysValue[i]==keyValue)
                    KeysValue[i]=0;
            KeysValue[1] = keyValue;
            tc.UnLock();
        }
        void DirectionControl::setkey_left(char keyValue)
        {
            tc.Lock();
            KeyUp(keyValue);
            for(int i=0;i<4;++i)
                if(KeysValue[i]==keyValue)
                    KeysValue[i]=0;
            KeysValue[2] = keyValue;
            tc.UnLock();
        }
        void DirectionControl::setkey_right(char keyValue)
        {
            tc.Lock();
            KeyUp(keyValue);
            for(int i=0;i<4;++i)
                if(KeysValue[i]==keyValue)
                    KeysValue[i]=0;
            KeysValue[3] = keyValue;
            tc.UnLock();
        }
   }
}
#endif